<template>
  <view class="content">
    <image class="logo" src="/static/logo.jpg"></image>
    <view>
      <text class="title">蓝牙打印演示</text>
    </view>
    <view class="input-wrap">
      <view>打印机名称：</view>
      <input class="input" @input="onKeyInput()" v-model="printerName" placeholder="输入打印机名称">
      <view class="small-text">将自动连接名称为 {{printerName}} 的打印机</view>
    </view>
    <view class="mt-1">
      <view>快捷选择：</view>
      <button v-for="(value, name, index) in items" @click="select(name, value)" size="mini" class="ml-1">{{name}}:{{value}}</button>
    </view>
    <view class="mt-1">
      <button type="default" @click="scan()">扫码绑定</button>
    </view>
    <view class="text-area">
      <navigator url="/pages/cpcl/index" class="mt-1">CPCL 打印测试</navigator>
      <navigator url="/pages/pos/index" class="mt-1">POS/ESC 打印测试</navigator>
    </view>
  </view>
</template>

<script>

  export default {
    data() {
      return {
        printerName: 'DP-HT303-',
        items: {}
      }
    },

    onShow() {
      this.items = uni.getStorageSync('items')
      console.debug('items', this.items)
    },

    methods: {
      
      onKeyInput() {
        uni.setStorageSync('printerName', this.printerName)
        console.debug('打印机名称：', this.printerName)
      },
      
      select(name, value) {
        console.debug('select', name, value)
        this.printerName = value
        uni.setStorageSync('printerName', value)
        
        uni.navigateTo({
        	url: `/pages/${name}/index`
        });
      },
      
      scan() {
        uni.scanCode({
          success: res => {
            const match = res.result.split('&')
            const name = match[0]
            uni.setStorageSync('printerName', name)
            this.printerName = name
          }
        })
      }
    
    }
  }
</script>

<style>
  .content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }

  .logo {
    height: 200rpx;
    width: 200rpx;
    margin-top: 200rpx;
    margin-left: auto;
    margin-right: auto;
    margin-bottom: 50rpx;
  }

  .text-area {
    margin-top: 0.75rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .title {
    font-size: 36rpx;
    text-align: center;
  }
  
  .small-text {
    font-size: 0.8rem;
    color: #999;
  }
  
  .input {
    outline: solid 1px #eee;
    margin-top: 0.25rem;
    margin-bottom: 0.25rem;
    padding: 0.5rem 0.75rem;
  }
  
  .mt-1 {
    margin-top: 0.75rem;
  }
  
  .ml-1 {
    margin-left: 0.75rem;
  }
  
  .input-wrap {
    margin-top: 1.5rem;
    width: 100%;
    box-sizing: border-box;
    padding-left: 2rem;
    padding-right: 2rem;
  }
</style>
